package com.example.learn;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

public class TagsActivity extends AppCompatActivity {

    private RecyclerView mainRecyclerView = null;
    private TagsAdapter mainAdapter = new TagsAdapter(MainActivity.tagsArrayList);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tags);
        mainRecyclerView = findViewById(R.id.mainRecyclerView);

        //设置一个布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecyclerView.setLayoutManager(linearLayoutManager);
        //设置对应的适配器
        mainRecyclerView.setAdapter(mainAdapter);

        mainAdapter.setOnItemClickListener(new TagsAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, TagsData bean) {
                Intent intent = new Intent();
                intent.putExtra("url", bean.getUrl());
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onDelete(int position, TagsData bean) {
                MainActivity.tagsArrayList.remove(position);
                mainAdapter.notifyDataSetChanged();
                MainActivity.editor.putString("tags", new Gson().toJson(MainActivity.tagsArrayList));
                MainActivity.editor.apply();
            }
        });

    }

}
